package com.allwees.data.analysis.product.api;

import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.core.common.util.DateUtil;
import com.allwees.core.common.util.MoneyUtil;
import com.allwees.data.analysis.base.api.BaseAnalysisApi;
import com.allwees.data.analysis.product.vo.ProductStatisticsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/data/api/analysis/product")
public class ProductApi  extends BaseAnalysisApi {

    /**
     * 商品数据统计
     * @return
     */
    @GetMapping("/product_statistic_data")
    public R productStatisticData() {
        ProductStatisticsVo productStatisticsVo = new ProductStatisticsVo();
        Date dateTo = DateUtil.getPreDay(DateUtil.getDayLast(new Date()));
        //昨日上新
        productStatisticsVo.setYesNewProdNum(query("product/PRODUCT_PUBLISH.txt",  DateUtil.addHour(dateTo,-24), dateTo));
        //在售商品数
        productStatisticsVo.setProdPutOnNum(query("product/PRODUCT_PUTON.txt", DateUtil.getDate(2020,0,1), new Date()));
        //在售SKU数
        productStatisticsVo.setTotalSaleSkuNum(query("product/SKU_SALE.txt", DateUtil.addHour(dateTo,-24),  dateTo));
        //有销量商品数
        productStatisticsVo.setProductPayedNum(query("product/PRODUCT_SALE.txt", DateUtil.getDate(2020,0,1), new Date()));
        //有销量的商品数占比
        //log.info("==============ProductPayedNum：{}  ==========ProdPutOnNum:{}",productStatisticsVo.getProductPayedNum().getClass(),productStatisticsVo.getProdPutOnNum().getClass());
        if((Integer)productStatisticsVo.getProdPutOnNum() != 0) {
            productStatisticsVo.setProdPayedRate(MoneyUtil.div((BigDecimal)productStatisticsVo.getProductPayedNum(),new BigDecimal((Integer)productStatisticsVo.getProdPutOnNum())));
        }

        // 活跃商户数(有在售商品的商家数)
        productStatisticsVo.setActiveRetailerNum(query("retailer/RETAILER_ACTIVE.txt", DateUtil.getDate(2020,0,1), new Date()));
        return  R.ok(productStatisticsVo);
    }

}
